package cn.edu.bs.service.impl;

import cn.edu.bs.dao.mapper.UserMapper;
import cn.edu.bs.domain.User;
import cn.edu.bs.domain.UserExample;
import cn.edu.bs.domain.custom.UserCustom;
import cn.edu.bs.service.UserService;
import cn.edu.bs.web.request.user.UserFormCommand;
import cn.edu.bs.web.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("userService")
public class UserServiceImpl implements UserService {


    private final UserMapper userMapper;

    @Autowired
    public UserServiceImpl(@Qualifier("userMapper") UserMapper userMapper) {
        this.userMapper = userMapper;
    }


    @Override
    public long countByExample(UserExample example) {
        return userMapper.countByExample(example);
    }

    @Override
    public int deleteByExample(UserExample example) {
        return userMapper.deleteByExample(example);
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return userMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(User record) {
        return userMapper.insert(record);
    }

    @Override
    public int insertSelective(User record) {
        return userMapper.insertSelective(record);
    }

    @Override
    public List<User> selectByExample(UserExample example) {
        return userMapper.selectByExample(example);
    }

    @Override
    public User selectByPrimaryKey(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByExampleSelective(User record, UserExample example) {
        return userMapper.updateByExampleSelective(record, example);
    }

    @Override
    public int updateByExample(User record, UserExample example) {
        return userMapper.updateByExample(record, example);
    }

    @Override
    public int updateByPrimaryKeySelective(User record) {
        return userMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(User record) {
        return userMapper.updateByPrimaryKey(record);
    }

    @Override
    public User transformCommandToEntity(UserFormCommand command) {
        User user = new User();
        BeanUtils.copyProperties(command, user);
        return user;
    }

    @Override
    public UserVO transformEntityToVO(User entity) {
        UserVO vo = new UserVO();
        BeanUtils.copyProperties(entity, vo);
        return vo;
    }

    @Override
    public UserVO transformCustomToVO(UserCustom custom) {
        UserVO vo = new UserVO();
        BeanUtils.copyProperties(custom, vo);
        return vo;
    }

    @Override
    public List<UserVO> transformEntitiesToTOs(List<User> entities) {
        List<UserVO> vos = new ArrayList<>();
        for (User user : entities) {
            vos.add(transformEntityToVO(user));
        }
        return vos;
    }

    @Override
    public List<UserVO> transformCustomsToTOs(List<UserCustom> customs) {
        List<UserVO> vos = new ArrayList<>();
        for (User user : customs) {
            vos.add(transformEntityToVO(user));
        }
        return vos;
    }

    @Override
    public User selectByUsername(String username) {
        UserExample example = new UserExample();
        example.createCriteria().andUsernameEqualTo(username);
        List<User> users = selectByExample(example);
        if (users == null || users.size() != 1) {
            return null;
        }
        return users.get(0);
    }
}
